<?php echo $this->headScript()->captureStart()?>

function setForm(data){
	console.dir(data);
	$("#title").attr('value',data.title);
	if (data.parent_id != null)
		$("#parent_id option[@value='"+data.parent_id+"']").attr('selected','selected');
	$("#parent_id option").removeAttr('disabled');
	if (data.resource_id != null)
		$("#parent_id option[@value='"+data.resource_id+"']").attr('disabled','true');
	if (data.kind == 'module')
		$("#parent_id option[@value='']").attr('disabled','true');
	$("#res_id").attr('value',data.resource_id);
	$("#sel_id").attr('value',sel_id);


	$("#tabsEx1").tabs('select',0);
	if (data.kind == 'menu') {
		$("#module_name").html('--');
		$("#permDetail").empty();
		$("#tabsEx1 > ul").tabs('disable',1);
		$("#tabsEx1 > ul").tabs('disable',2);

	}
	else{
		$("#module_name").html(data.resource_id).css({'color':'#00f','font-size':'18px'});
		$("#tabsEx1").tabs('enable',1);
		$("#tabsEx1").tabs('enable',2);
		modDetail(data.resource_id); // 授權項目
	}

}

// 模組資料
function modset(id){
	$.getJSON("<?php echo $this->baseScript?>/getform/id/"+id ,
		function(data){
			setForm(data);
		}
	);
	$("#setModule").show();
}

// 取回授權項目
function modDetail(id) {
	$.get("<?php echo $this->baseScript?>/getitems/id/"+id ,
		function(data){
			$("#permDetail").html(data);
		//$("ul.tree").checkTree({});
		//	$("ul.tree div.checkbox").addClass('checked');
		}
	);

}
// 授權資料
function aclset(id) {

	$.get("<?php echo $this->baseScript?>/getaclaccess/id/"+id ,
		function(data){
			$("#permList").html(data);
			show_aclrow();
			$("#permList .delacl").click(function(){
				if(confirm('確定刪除?')) {
				var d_id = $(this).attr('id').substr(2);
				delacl(d_id); // 刪除權限

				}
			});
		}
	);
}

function show_aclrow(){
	$("#permList tbody tr").removeClass();
	$("#permList tbody tr:even").addClass('even');
	$("#permList tbody tr:odd").addClass('odd');
	//$("#permList span.delacl").css({'cursor': 'pointer','background-color':'#f6f','color':'#fff','padding':'0 2px'});
}


// 刪除權限
function delacl(id) {
	$.getJSON("<?php echo $this->baseScript?>/delaccess/id/"+id ,
		function(data) {
			$("#d-"+data).parent().parent().remove();
		}
	);
	show_aclrow();
}


function setItemOption(data) {
	$("#temp_id").empty();
	$.each(data.items,function(){
		$("#temp_id").append('<option value="'+this.sn+'">'+this.name+'</option>');
	})

}

$(document).ready(function(){
	$('#setform').ajaxForm(function(data) {
		if (data == 'reload') {
			location.reload();
		}
		else{
			$("#title-"+data).html ($("#title").attr('value'));
	      $("#setmessage").html('設定完成!').show().fadeOut('show');
	      aclset(sel_id); // 重新顯示授權資料
      }
     });

	$("#id_kind").change(function(){
			var id = $(this).attr('value');
			$.getJSON("<?php echo $this->route('getitem')?>", { kind: id },
		 	function(json){
	  			setItemOption(json); //顯示權限項目
			  	if (json.items.length>0){
				$("#temp_id").show();
				$("#allow_select").show();
				$("#permDetail").show('slow');
				}
				else {
					$("#temp_id").empty();
					$("#temp_id").hide();
					$("#allow_select").hide();
					$("#permDetail").hide('slow');
				}
			});
		});


});
<?php echo $this->headScript()->captureEnd()?>
<form id="setform"
	action='<?php echo $this->url(array('controller'=>'manager','action'=>'edit'))?>'
	method='post'>
<table id="m-set">
	<tr>
		<th>模組名稱</th>
		<td><span id="module_name"></span></td>
	</tr>
	<tr>
		<th>顯示名稱</th>
		<td><input type='text' name='title' id='title' value=''></td>
	</tr>
	<tr>
		<th>所屬分類</th>
		<td>

		<select id='parent_id' name='parent_id'>
		<option value="">首頁</option>
		<?php foreach ($this->moduleMenu as $row): ?>
  		<option value="<?php echo $row['resource_id']?>"><?php echo $this->escape($row['title']) ?></option>
  		<?php endforeach; ?>
		</select></td>
	</tr>
	<tr>
		<th>權限指派</th>
		<td><select name="id_kind" id="id_kind" size="1"
			style="background-color: #FFFFFF; font-size: 13px">
			<option value=""> -- </option>
			<?php foreach ($this->roleKind as $id=>$val):?>
			<option value="<?php echo $id ?>"><?php echo $val ?></option>
			<?php endforeach ?>
		</select>
		<select name="setid" id="temp_id" style="display: none">
		</select>
		<select name="allow" id="allow_select" style="display: none"><option value="1">允許存取</option><option value="0">限制存取</option></select>
		 <input type='submit' name='act' value='儲存'></td>
	</tr>
	</table>
	<div id="permDetail" style="display: none"></div>

	<hr/>
	<div id="permList"></div>

<input type="hidden" id="res_id" name="res_id" value="">
<input type="hidden" id="sel_id" name="access_id" value="">
</form>